var app = new Vue({
    el: "#app",
    data: {
        form: {
            mz: '01',        //民族
            csd: '37',       //出生地


            dist1: 37e4,
            dist2: 3706e2,
            hkszd: 370602,   //户口所在地
            sfzh: "",      //身份证号
            lxdh: "",      //联系电话
            zwx: "",       //中文姓
            zwm: "",       //中文名
            ywx: "",       //英文姓
            ywm: "",       //英文名
            jjqklxr: "",   //紧急情况联系人
            jjqklxrdh: "", //紧急情况联系人电话
            sqlbArray: [],       //申请类别
            qwdArray: [],         //前往地
            sqlb: "",
            qwd: "",


            bzlbHz: '11',   //普通护照办证类别
            xczjhmHz: "",
            xczjyxqzHz: null,
            cymjz: "",
            xmjz: "",
            cczjz: "",

            bzlbGa: '11',   //港澳通行证办证类别
            xczjhmGa: "",
            xczjyxqzGa: null,
            qzzlHkg: '12',   //香港签注种类
            qzzlMac: '12',   //澳门签注种类
            qzcsHkg: '0301', //香港签注次数
            qzcsMac: '0301', //澳门签注次数


            bzlbTw: '11',   //台湾通行证办证类别
            xczjhmTw: "",
            xczjyxqzTw: null,
            qzzlTwn: '25',   //台湾签注种类
            qzcsTwn: '0601', //台湾签注次数

            sfxtkzd: '0',
            sjr: "",
            yzbm: "",
            emsdz: "",
            sjhm: "",
            sqzjhqzGa: "11",
            sqzjhqzTw: "11"


        },
        gaswqzAlertFlag:false,

        mzOptions: mzOptions,
        csdOptions: csdOptions,
        distOptions1: distData[1e5],
        bzlbOptionsHz: [{key: '11', value: '首次申领'}, {key: '22', value: '补发'}, {key: '31', value: '换发'}, {
            key: '13',
            value: '失效重新申领'
        }, {key: '71', value: '持证申请加注'}],
        bzlbOptionsGa: [{key: '11', value: '首次申领'}, {key: '22', value: '补发'}, {key: '31', value: '换发'}, {
            key: '13',
            value: '失效重新申领'
        }],
        bzlbOptionsTw: [{key: '11', value: '首次申领'}, {key: '22', value: '补发'}, {key: '31', value: '换发'}, {
            key: '13',
            value: '失效重新申领'
        }],
        qzzlHkgOptions: [{key: '12', value: '团队旅游'}, {key: '13', value: '商务'}, {key: '11', value: '探亲'}, {
            key: '1G',
            value: '逗留'
        }, {key: '19', value: '其他'}],
        qzzlMacOptions: [{key: '12', value: '团队旅游'}, {key: '13', value: '商务'}, {key: '11', value: '探亲'}, {
            key: '1G',
            value: '逗留'
        }, {key: '19', value: '其他'}],
        qzzlTwnOptions: [{key: '25', value: '团队旅游'}, {key: '2B', value: '个人旅游'}, {key: '21', value: '探亲'}, {
            key: '27',
            value: '商务'
        }, {key: '22', value: '应邀'}, {key: '23', value: '定居'}, {key: '2A', value: '就学'}, {
            key: '26',
            value: '乘务'
        }, {key: '29', value: '其他'}],
        sqzjhqzOptions: [{key: '11', value: '申请证件和签注'}, {key: '10', value: '仅申请证件'}, {key: '01', value: '仅申请签注'}]
    },
    methods: {
        onGaqzChange: function (e) {

            if ((checkContains(this.form.qwdArray, "HKG") && this.form.qzzlHkg == '13' && this.form.qzcsHkg.substring(2, 4) != '01') ||
                (checkContains(this.form.qwdArray, "MAC") && this.form.qzzlMac == '13' && this.form.qzcsMac.substring(2, 4) != '01')){
                if (!this.gaswqzAlertFlag)
                {
                    showDialog('申请港澳多次商务签注，企业机构须事先向所在地有审批权的公安机关出入境管理部门备案。');
                    this.gaswqzAlertFlag =true;
                }

            }
            else {
                this.gaswqzAlertFlag =false;

            }
        },
        onTwqzChange:function(e)
        {
            if (this.form.qzzlTwn == '25' && this.form.qzcsTwn.substring(2, 4) != '01')
            {
                showDialog("赴台团队旅游多次签注，申请对象仅限赴台旅游组团社领队！");
            }

        },
        check: function (e) {
            if (this.validateInput()) {
                var that = this;
                // this.form.qwd = this.form.qwdArray.join(",");
                // this.form.sqlb = this.form.sqlbArray.join(",");
                // alert(JSON.stringify(this.form));
                $.post("/zggmtb/check", this.form, function (res) {
                    if (res.result == "ok") {
                        $("#zggmtbForm").submit();
                    }
                    else if (res.result == 'tips') {
                        showTips(res.msg);
                    }
                    else if (res.result == 'error') {
                        showDialog(res.msg);
                    }
                });
            }
        },
        submit: function () {
            $("#zggmtbForm").submit();
        },
        validateInput: function () {

            var tempSqlbArray = [];
            if (checkContains(this.form.sqlbArray, "101")) {
                tempSqlbArray.push("101");
            }

            if (checkContains(this.form.sqlbArray, "102")) {
                if (this.form.sqzjhqzGa == '11' || this.form.sqzjhqzGa == '10') {
                    tempSqlbArray.push("102");
                }
            }
            if (checkContains(this.form.sqlbArray, "104")) {
                if (this.form.sqzjhqzTw == '11' || this.form.sqzjhqzTw == '10') {
                    tempSqlbArray.push("104");
                }
            }
            var tempQwdArray = []

            if (checkContains(this.form.sqlbArray, "102")) {
                if (this.form.sqzjhqzGa == '11' || this.form.sqzjhqzGa == '01') {
                    if (checkContains(this.form.qwdArray, "HKG")) {
                        tempQwdArray.push("HKG")
                    }
                    if (checkContains(this.form.qwdArray, "MAC")) {
                        tempQwdArray.push("MAC")
                    }
                }
            }
            if (checkContains(this.form.sqlbArray, "104")) {
                if (this.form.sqzjhqzTw == '11' || this.form.sqzjhqzTw == '01') {
                    tempQwdArray.push("TWN")
                }
            }

            this.form.qwd = tempQwdArray.join(",");
            this.form.sqlb = tempSqlbArray.join(",");


            if (!checkIdCard(this.form.sfzh)) {
                showDialog("请输入正确的身份证号!");
                return false;
            }
            if (!checkPhone(this.form.sjhm)) {
                showDialog("请输入正确的手机号码!");
                return false;
            }
            if (!checkNull(this.form.zwm, this.form.zwx, this.form.ywm, this.form.ywx)) {
                showDialog("请输入正确的姓名!");
                return false;
            }

            if (!checkNull(this.form.jjqklxr)) {
                showDialog("请输入正确的紧急情况联系人!");
                return false;
            }
            if (!checkNull(this.form.jjqklxrdh)) {
                showDialog("请输入正确的紧急情况联系人电话!");
                return false;
            }

            if (this.form.jjqklxrdh == this.form.sjhm) {
                showDialog("紧急情况联系人电话请填写与申请人本人不同的手机号码!");
                return false;
            }

            if (tempSqlbArray.length == 0 && tempQwdArray.length == 0) {
                showDialog("请至少选择一项要办理的证件信息！");
                return false;
            }
            if (checkContains(this.form.sqlbArray, "101")) {
                if (this.form.bzlbHz != '11') {
                    // if (!checkNull(this.form.xczjhmHz)) {
                    //
                    //     showDialog("请输入正确的护照号码!");
                    //     return false;
                    // }
                }
                if (this.form.bzlbHz == '71') {
                    if (!checkNull(this.form.cymjz) && !checkNull(this.form.xmjz) && !checkNull(this.form.cczjz)) {
                        showDialog("请输入正确的护照加注信息!");
                        return false;
                    }
                }

            }

            if (checkContains(this.form.sqlbArray, "102")) {
                //办理证件和签注或者仅办证
                if (this.form.sqzjhqzGa == '11' || this.form.sqzjhqzGa == '10') {
                    if (this.form.bzlbGa != '11') {
                        // if (!checkNull(this.form.xczjhmGa)) {
                        //     showDialog("请输入正确的往来港澳通行证号码!");
                        //     return false;
                        // }
                    }
                }
                //仅签注
                if (this.form.sqzjhqzGa == '01') {
                    // if (!checkNull(this.form.xczjhmGa)) {
                    //     showDialog("请输入正确的往来港澳通行证号码!");
                    //     return false;
                    // }
                }

                //办理证件和签注或者仅签注
                if (this.form.sqzjhqzGa == '11' || this.form.sqzjhqzGa == '01') {
                    if (!checkContains(tempQwdArray, 'HKG') && !checkContains(tempQwdArray, 'MAC')) {
                        if (!checkNull(this.form.xczjhmGa)) {
                            showDialog("请填写正确的往来港澳签注信息!");
                            return false;
                        }
                    }
                }


            }

            if (checkContains(this.form.sqlbArray, "104")) {
                //办理证件和签注或者仅办证
                if (this.form.sqzjhqzTw == '11' || this.form.sqzjhqzTw == '10') {
                    if (this.form.bzlbTw != '11') {
                        // if (!checkNull(this.form.xczjhmTw)) {
                        //     showDialog("请输入正确的往来台湾通行证号码!");
                        //     return false;
                        // }
                    }
                }
                //仅签注
                if (this.form.sqzjhqzTw == '01') {
                    // if (!checkNull(this.form.xczjhmTw)) {
                    //     showDialog("请输入正确的往来港澳通行证号码!");
                    //     return false;
                    // }
                }

            }


            return true;
        }
    },
    computed: {
        ywx: {
            set: function (newValue) {
                this.form.ywx = newValue.toUpperCase();
            },
            get: function () {
                return this.form.ywx;
            }
        },
        ywm: {
            set: function (newValue) {
                this.form.ywm = newValue.toUpperCase();
            },
            get: function () {
                return this.form.ywm;
            }
        },
        xczjhmHz: {
            set: function (newValue) {
                this.form.xczjhmHz = newValue.toUpperCase();
            },
            get: function () {
                return this.form.xczjhmHz;
            }
        },

        xczjhmGa: {
            set: function (newValue) {
                this.form.xczjhmGa = newValue.toUpperCase();
            },
            get: function () {
                return this.form.xczjhmGa;
            }
        },

        xczjhmTw: {
            set: function (newValue) {
                this.form.xczjhmTw = newValue.toUpperCase();
            },
            get: function () {
                return this.form.xczjhmTw;
            }
        },
        formJson: function () {
            return JSON.stringify(this.form);
        },
        distOptions2: function () {
            if (this.form.dist1 == 37e4) {
                this.form.dist2 = 3706e2;
            }
            else {
                for (p in distData[this.form.dist1]) {
                    this.form.dist2 = p;
                    break;
                }
            }
            return distData[this.form.dist1];
        },
        distOptions3: function () {
            if (this.form.dist2 == 3706e2) {
                this.form.hkszd = 370602;
            }
            else {
                for (p in distData[this.form.dist2]) {
                    this.form.hkszd = p;
                    break;
                }
            }

            return distData[this.form.dist2];
        },
        distName1: function () {
            return distName(this.form.dist1);
        },
        distName2: function () {
            return distName(this.form.dist2);
        },
        distName3: function () {
            return distName(this.form.hkszd);
        },
        idNumber:{
            get:function()
            {
                return this.form.sfzh;
            },
            set:function (newValue) {
                this.form.sfzh = newValue.toUpperCase();
            }
        },
        zwx: {
            set: function (newValue) {
                this.form.ywx = convertPinYin(newValue);

                if ((this.form.zwx + this.form.zwm) == this.form.sjr) {
                    this.form.sjr = newValue + this.form.zwm;
                }
                this.form.zwx = newValue;
            },
            get: function () {
                return this.form.zwx;
            }
        },
        zwm: {
            set: function (newValue) {
                this.form.ywm = convertPinYin(newValue);
                if ((this.form.zwx + this.form.zwm) == this.form.sjr) {
                    this.form.sjr = this.form.zwx + newValue;
                }
                this.form.zwm = newValue;
            },
            get: function () {
                return this.form.zwm;
            }
        },
        lxdh: {
            set: function (newValue) {
                if (this.form.lxdh == this.form.sjhm) {
                    this.form.sjhm = newValue;
                }
                this.form.lxdh = newValue;
            },
            get: function () {
                return this.form.lxdh;
            }
        },

        /**
         * 办理护照内容显示
         */
        blhzDisplay: function () {
            if (checkContains(this.form.sqlbArray, '101')) {
                return false;
            }
            else {
                return true;
            }
        },
        /**
         * 护照资料内容显示
         */
        hzzlDisplay: function () {
            if (checkContains(this.form.sqlbArray, '101')) {
                if (this.form.bzlbHz != '11') {
                    return false;
                }
                else {
                    return true;
                }
            }
            else {
                return true;
            }
        },
        /**
         * 护照加注内容显示
         */
        hzjzDisplay: function () {
            if (checkContains(this.form.sqlbArray, '101')) {
                if (this.form.bzlbHz == '71') {
                    return false;
                }
                else {
                    return true;
                }
            }
            else {
                return true;
            }
        },
        sqzjhqzGaDisplay: function () {
            if (checkContains(this.form.sqlbArray, '102')) {
                return false;
            }
            else {
                return true;
            }
        },
        blgaDisplay: function () {
            if (checkContains(this.form.sqlbArray, '102')) {
                if (this.form.sqzjhqzGa == '11' || this.form.sqzjhqzGa == '10') {
                    return false;
                }
            }
            return true;
        },
        qzgaDisplay: function () {
            if (checkContains(this.form.sqlbArray, '102')) {
                if (this.form.sqzjhqzGa == '11' || this.form.sqzjhqzGa == '01') {
                    return false;
                }
            }
            return true;
        },
        gazlDisplay: function () {
            if (checkContains(this.form.sqlbArray, '102')) {
                //办理证件且办证类型不为首次申领
                if ((this.form.sqzjhqzGa == '11' || this.form.sqzjhqzGa == '10') && this.form.bzlbGa != '11') {
                    return false;
                }
                if (this.form.sqzjhqzGa == '01') {
                    return false;
                }

            }
            return true;
        },
        xgqzDisplay: function () {
            if (checkContains(this.form.sqlbArray, '102')) {
                if (this.form.sqzjhqzGa == '11' || this.form.sqzjhqzGa == '01') {
                    if (checkContains(this.form.qwdArray, 'HKG')) {
                        return false;
                    }
                }
            }

            return true;
        },
        amqzDisplay: function () {
            if (checkContains(this.form.sqlbArray, '102')) {
                if (this.form.sqzjhqzGa == '11' || this.form.sqzjhqzGa == '01') {
                    if (checkContains(this.form.qwdArray, 'MAC')) {
                        return false;
                    }
                }
            }
            return true;
        },
        bltwDisplay: function () {
            if (checkContains(this.form.sqlbArray, '104')) {
                if (this.form.sqzjhqzTw == '11' || this.form.sqzjhqzTw == '10') {
                    return false;
                }
            }
            return true;

        },
        sqzjhqzTwDisplay: function () {
            if (checkContains(this.form.sqlbArray, '104')) {
                return false;
            }
            return true;
        },
        twzlDisplay: function () {


            if (checkContains(this.form.sqlbArray, '104')) {
                //办理证件且办证类型不为首次申领
                if ((this.form.sqzjhqzTw == '11' || this.form.sqzjhqzTw == '10') && this.form.bzlbTw != '11') {
                    return false;
                }
                if (this.form.sqzjhqzTw == '01') {
                    return false;
                }

            }
            return true;
        },
        twqzDisplay: function () {
            if (checkContains(this.form.sqlbArray, '104')) {

                if (this.form.sqzjhqzTw == '11' || this.form.sqzjhqzTw == '01') {
                    return false;
                }

            }
            return true;
        },
        emsDisplay: function () {
            if (this.form.sfxtkzd == '1') {
                return false;
            } else {
                return true;
            }
        },
        qzcsHkgOptions: function () {
            var result = [];
            switch (this.form.qzzlHkg) {
                case '12'://团队旅游
                    result = [{key: '0301', value: '3个月一次'}, {key: '0302', value: '3个月二次'}, {
                        key: '1201',
                        value: '1年一次'
                    }, {key: '1202', value: '1年二次'}];
                    break;
                // case '1B'://个人旅游
                //     result = [{key:'0301',value:'3个月一次'},{key:'0302',value:'3个月二次'},{key:'1201',value:'1年一次'},{key:'1202',value:'1年二次'},,{key:'1299',value:'1年多次'}];
                //     break;
                case '13'://商务
                case '11'://探亲
                    result = [{key: '0301', value: '3个月一次'}, {key: '0399', value: '3个月多次'}, {
                        key: '1299',
                        value: '1年多次'
                    }];
                    break;
                case '19'://其他
                    result = [{key: '0301', value: '3个月一次'}, {key: '0302', value: '3个月二次'}, {
                        key: '0399',
                        value: '3个月多次'
                    }];
                    break;
                case '1G':
                    result = [{key: '9999', value: '多次有效'}];
                    break;

            }
            this.form.qzcsHkg = result[0].key;
            return result;
        },
        qzcsMacOptions: function () {
            var result = [];
            switch (this.form.qzzlMac) {
                case '12'://团队旅游
                    result = [{key: '0301', value: '3个月一次'}, {key: '1201', value: '1年一次'}];
                    break;
                // case '1B'://个人旅游
                //     result = [{key:'0301',value:'3个月一次'},{key:'1201',value:'1年一次'}];
                //     break;
                case '13'://商务
                case '11'://探亲
                    result = [{key: '0301', value: '3个月一次'}, {key: '0399', value: '3个月多次'}, {
                        key: '1299',
                        value: '1年多次'
                    }];
                    break;
                case '19'://其他
                    result = [{key: '0301', value: '3个月一次'}, {key: '0302', value: '3个月二次'}, {
                        key: '0399',
                        value: '3个月多次'
                    }];
                    break;
                case '1G':
                    result = [{key: '9999', value: '多次有效'}];
                    break;

            }
            this.form.qzcsMac = result[0].key;
            return result;

        },
        qzcsTwnOptions: function () {

            var result = [];
            switch (this.form.qzzlTwn) {
                case '21'://探亲
                case '22'://应邀
                case '27'://商务
                case '2A'://就学
                    result = [{key: '0601', value: '6个月一次'}, {key: '1299', value: '1年多次'}, {
                        key: '2499',
                        value: '2年多次'
                    }, {key: '3699', value: '3年多次'}];
                    break;
                case '26'://乘务
                    result = [{key: '1299', value: '1年多次'}, {key: '2499', value: '2年多次'}, {key: '3699', value: '3年多次'}];
                    break;
                case '25'://团队旅游
                case '29'://其他
                    result = [{key: '0601', value: '6个月一次'}, {key: '1299', value: '一年多次'}];
                    break;
                case '23'://赴台定居
                case '2B'://个人旅游
                    result = [{key: '0601', value: '6个月一次'}];
                    break;
            }
            this.form.qzcsTwn = result[0].key;
            return result;
        },


    },

    created: function () {
        //this.form.xczjyxqzHz = nowString();
        //this.form.xczjyxqzGa = nowString();
        //this.form.xczjyxqzTw = nowString();
    }
});


$(document).ready(function () {
    var u = navigator.userAgent;
    if (u.indexOf('Android') > -1) {
        $("#distMask").hide();
    }
});

function showTips(tips) {
    $("#tipsContent").text(tips);
    $("#iosDialog1").fadeIn(200);
}

function hideDialog1() {
    $("#iosDialog1").fadeOut(200);
}

function submitForm() {
    app.submit();
    hideDialog1();
}

function showDialog2() {
    $("#iosDialog2").fadeIn(200);
}

function hideDialog2() {
    $("#iosDialog2").fadeOut(200);
}

function importInfo() {
    hideDialog2();
    app.idNumber = cUser.idNumber;
    app.lxdh = cUser.phoneNumber;
    app.zwx = cUser.realName.substring(0,1);
    app.zwm = cUser.realName.substring(1);
}